#include "admin.h"
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iostream>
using std::find;
using std::ifstream;
using std::istringstream;


double Admin::getUserMoney(const string& id) const{
	vector<User>::iterator iter = findUserById(id);
	if (iter == myAtm->users.end())
		return -1;
	else
		return iter->money();
}

bool Admin::addNewUser(const User& u) const{
	vector<User>::iterator iter = findUserById(u.getId());
	if (iter !=myAtm->users.end())
		return false;
	else
		myAtm->users.push_back(u);
	return true;
}


bool Admin::delUser(const string& id) const{
	vector<User>::iterator iter = findUserById(id);
	if (iter == myAtm->users.end())
		return false;
	else
		myAtm->users.erase(iter); 
	return true;
}


string Admin::getUserPassword(const string& id) const{
	vector<User>::iterator iter = findUserById(id);
	if (iter == myAtm->users.end())
		return "";  // user not find
	else
		return iter->getPassword();
}
vector<User>::iterator Admin::findUser(const User& u) const{
	return 
		find(myAtm->users.begin(), myAtm->users.end(), u);
}

vector<User>::iterator Admin::findUserById(const string& id) const{
	vector<User>::iterator iter = myAtm->users.begin();
	for (; iter != myAtm->users.end(); ++iter)
		if (iter->getId() == id) break;
	return iter;
}

void Admin::showAllUserInfo(std::ostream &os ) const{
	os << "ID\t\tMoney" << std::endl;
	vector<User>::iterator iter = myAtm->users.begin();
	for (; iter != myAtm->users.end(); ++iter)
		os << iter->getId() << "\t\t" << iter->money() << std::endl;
}

